package com.xd.common.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.xd.core.system.entity.LoginUser;
import com.xd.common.util.SecurityUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        LoginUser user = SecurityUtil.getLoginUser0();
        //属性名称，不是字段名称
        this.setFieldValByName("createTime", new Date(), metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);
        this.setFieldValByName("orderTime", new Date(), metaObject);
        if (null != user) {
            this.setFieldValByName("createBy", user.getUser().getId(), metaObject);
            this.setFieldValByName("updateBy", user.getUser().getId(), metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        LoginUser user = SecurityUtil.getLoginUser0();
        this.setFieldValByName("updateTime", new Date(), metaObject);
        if (null != user)
            this.setFieldValByName("updateBy", user.getUser().getId(), metaObject);
    }
}

